package com.young.fragments.demo.show_hide_demo

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.young.fragments.databinding.ActivitySample2Binding
import com.young.fragments.demo.show_hide_demo.fms.ShowHidePage1
import com.young.fragments.demo.show_hide_demo.fms.ShowHidePage2
import com.young.fragments.demo.show_hide_demo.fms.ShowHidePage3
import com.young.lib_fragments.acs.BaseActivity

/**
 * @author huangzehui
 * @date 2025/1/23 13:02 周四
 * @description 展示隐藏 demo
 */
class ShowHideDemoActivity : BaseActivity() {

    private val mBinding by lazy {
        ActivitySample2Binding.inflate(layoutInflater)
    }

    // 上一个展示的 fragment
    private var mLastFm: Fragment? = null

    private var mShowHidePage1: ShowHidePage1? = null
    private var mShowHidePage2: ShowHidePage2? = null
    private var mShowHidePage3: ShowHidePage3? = null


    override fun getFragmentContainerId(): Int {
        return mBinding.flFmContainer.id
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)

        initClick()

        showFirstFm()
    }


    private fun initClick() {
        val clickListener = View.OnClickListener {

            val fm: Fragment

            when (it) {
                mBinding.btnPage1 -> {
                    if (mShowHidePage1 == null) {
                        mShowHidePage1 = ShowHidePage1()
                    }
                    fm = mShowHidePage1!!
                }

                mBinding.btnPage2 -> {
                    if (mShowHidePage2 == null) {
                        mShowHidePage2 = ShowHidePage2()
                    }
                    fm = mShowHidePage2!!
                }


                mBinding.btnPage3 -> {
                    if (mShowHidePage3 == null) {
                        mShowHidePage3 = ShowHidePage3()
                    }
                    fm = mShowHidePage3!!
                }

                else -> {
                    throw Exception("没有相关的页面")
                }
            }

            if (fm == mLastFm) {
                return@OnClickListener
            }

//            FmTransitionsUtil.showAndHide(
//                fmManager = supportFragmentManager,
//                containerViewId = getFragmentContainerId(),
//                showFm = fm,
//                hideFm = mLastFm,
//            )

            showAndHide(
                showFm = fm,
                hideFm = mLastFm
            )

            mLastFm = fm

        }


        mBinding.btnPage1.setOnClickListener(clickListener)
        mBinding.btnPage2.setOnClickListener(clickListener)
        mBinding.btnPage3.setOnClickListener(clickListener)
    }

    private fun showFirstFm() {
        mBinding.btnPage1.performClick()
    }

}